function filemanager() {}
//открываем папку
filemanager.toggleTreeFolderOpen = function (idFolder) {
    $(".headFolderName").replaceWith("<p class=\"headFolderName\" id=\"" + idFolder + "\">" + $(this).find("#folder_a_" + idFolder).text() + "</p>");
    $("#folder_" + idFolder + " > div").show();
    $("#folder_" + idFolder).attr("class", "colapsable");
    $("#folder_" + idFolder + " .open:first").attr("href", "javascript:filemanager.toggleTreeFolderClose(" + idFolder + ");");
    filemanager.boldSelect(idFolder);
}
//закрываем папку
filemanager.toggleTreeFolderClose = function (idFolder) {
    $("#folder_" + idFolder + " div").hide();
    $("#folder_" + idFolder).attr("class", "expandable");
    $("#folder_" + idFolder + " .open:first").attr("href", "javascript:filemanager.toggleTreeFolderOpen(" + idFolder + ");");
    filemanager.boldSelect(idFolder);
}
//выбор папки
filemanager.selectFolder = function (idFolder) {
    filemanager.toggleTreeFolderOpen(idFolder);
    $.ajax({
        url: W_AJAX + "getFolder.php",
        dataType: "json",
        type: "POST",
        data: ({
            idFolder: idFolder
        }),
        beforeSend: function () {
            $("#ajaxLoader").show();
            $("#openFolder").empty();
        },
        success: function (data) {
            $("#ajaxLoader").hide();
            $("#openFolder").replaceWith(data);
        }
    });
}
//выделяем папку
filemanager.boldSelect = function (idFolder) {
    $(".boldSelect").removeClass();
    $("#folder_a_" + idFolder).attr("class", "boldSelect");
}
//получаем id открытой папки
filemanager.getFolderId = function () {
    return $(".headFolderName").attr("id");
}
//создаем папку
filemanager.createFolder = function () {
    var newFolderName = $("#nameOfNewFolder").val();
    var idFolder = filemanager.getFolderId();

    $.ajax({
        url: W_AJAX + "createFolder.php",
        dataType: "json",
        type: "POST",
        data: ({
            idFolder: idFolder,
            newFolderName: newFolderName
        }),
        beforeSend: function () {
            $("#f1_upload_process").show();
            $("#f1_upload_form").hide();
            $("#creationMessage").empty();
        },
        success: function (data) {
            $("#creationMessage").append("Папка создана успешно");
            $("#f1_upload_process").hide();
            $("#f1_upload_form").show();
            $("#nameOfNewFolder").val("");
            filemanager.reload(data);
        }
    });
}
//перегружаем все
filemanager.reload = function (data) {
    $("#openFolder").empty();
    $(".headFolderName").empty();
    $(".headFolderName").removeAttr("id");
    $("#tree .convert").empty();
    $("#tree .convert").append(data);
    tb_remove();
}
//включаем отображние загрузки


function upload() {};
upload.startUpload = function () {
    $(".f1_upload_process").show();
    $(".f1_upload_form").hide();
    $(".uploadMessage").empty();
    return true;
}
//выключаем отображние загрузки
upload.stopUpload = function (result) {
    $("#ajaxLoad").remove();
    $(".f1_upload_process").hide();
    $(".uploadMessage").append(result);
    //document.getElementById("f1_upload_form").innerHTML = result + "<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>";
    $(".f1_upload_form").show();
    return true;
}

function select() {};
//выделяем поля
select.selectRow = function (obj, table) {
    var id = $(obj).attr("id");
    if ($("#openFolder tr." + table + "Row#" + id).attr("class") == table + "Row select") {
        $("#openFolder tr." + table + "Row#" + id + " input:checkbox").attr("checked", false);
        $("#openFolder tr." + table + "Row#" + id).removeClass("select");
    } else {
        $("#openFolder tr." + table + "Row#" + id + " input:checkbox").attr("checked", true);
        $("#openFolder tr." + table + "Row#" + id).addClass("select");
    }
}
//выдяеляем все поля
select.selectAllRow = function (table) {
    $("#selectAll" + table).toggleClass("select");
    $("#openFolder ." + table + "Row").toggleClass("select");
    if ($("#selectAll" + table).attr("class") == "select") {
        $("#openFolder ." + table + "Row input:checkbox").attr("checked", true);
        $("#openFolder ." + table + "Row").addClass("select");
    } else {
        $("#openFolder ." + table + "Row input:checkbox").attr("checked", false);
        $("#openFolder ." + table + "Row").removeClass("select");
    }
}

function operation() {}
//удаляем полля
operation.deleteRow = function (table) {
    var arrIds = operation.getSelectIds(table);

    $.ajax({
        url: W_AJAX + "delete" + table + ".php",
        dataType: "json",
        type: "POST",
        data: ({
            arrIds: arrIds
        }),
        success: function () {
            filemanager.selectFolder(filemanager.getFolderId());
            if (table == 'folder') {
                $.each(arrIds, function (index, value) {
                    $("#folder_" + value[0]).remove();
                });
            }
        }
    });

}
//получаем id выбранных полей
operation.getSelectIds = function (table) {
    var arrElements = $("." + table + "Row:has(input:checkbox:checked)");
    var size = $("." + table + "Row:has(input:checkbox:checked)").size();
    var arrIds = new Array(size);

    if (size != 0) {
        for (i = 0; i < size; i++) {
            arrIds[i] = new Array(2);
            arrIds[i][0] = $(arrElements[i]).attr("id");
            arrIds[i][1] = $(arrElements[i]).find(".unique_name").text();
        }
        return (arrIds);
    } else {
        if (table == "file") var text = "Файл(ы)";
        else var text = "Папки";
        text += " не выбраны";
        alert(text);
    }
}

operation.getWavImage = function (uniqueName) {
   $.ajax({
        url: W_AJAX + "getWavImage.php",
        dataType: "json",
        type: "POST",
        data: ({
            uniqueName: uniqueName
        }),
        success: function (data) {
          if(data == 1){
            $('.forImage img').attr('src', W_ABSPATH + 'content/upload_files/' + uniqueName + '.png');
            $('.forImage img').show();
          }
          else{
            alert(data);
          }
        }
    });
}